//
//  PxBaseController.m
//  PxHelper
//
//  Created by lpc on 2019/10/18.
//  Copyright © 2019 lpc. All rights reserved.
//

#import "PxBaseController.h"

@interface PxBaseController ()

@end

@implementation PxBaseController

- (instancetype)init
{
    self = [super init];
    if (self) {
        // Custom initialization
        [self addEventObserver];
        NSLog(@"=====%@ init=====",NSStringFromClass([self class]));
    }
    return self;
}

-(instancetype)initWithSelfClassNib
{
    return [self initWithNibName:NSStringFromClass([self class]) bundle:nil];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self addEventObserver];
        NSLog(@"=====%@ init=====",NSStringFromClass([self class]));
    }
    return self;
}

-(void)dealloc{
    [self removeEventObserver];
    NSLog(@"=====%@ dealloc=====",NSStringFromClass([self class]));
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = APP_BG_COLOR;
    //[UIColor colorWithPatternImage:[UIImage imageNamed:@"Grobal_Background.png"]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;

    [self setNavBarLeftGoBackButtonItem];
}

- (void)setNavBarTitle:(NSString *)navBarTitle{
    self.navigationItem.title = navBarTitle;
}

-(void)addEventObserver
{
    [self removeEventObserver];
}

-(void)removeEventObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)goBack
{
    [self.view endEditing:YES];
    if (self.presentingViewController &&
        (!self.navigationController ||self == self.navigationController.viewControllers.firstObject)) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)setNavBarLeftGoBackButtonItem
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 44, 44);
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];

    UIView *view = [[UIView alloc] initWithFrame:btn.bounds];
    [view addSubview:btn];
    [self setNavBarLeftButtonItem:view];
}

-(void)setNavBarLeftButtonItem:(nonnull UIView *)leftButton
{
    //当时NavigationController 根控制器，且没有需要设置左视图时，返回。
    NSArray *array = self.navigationController.viewControllers;
    if (array.firstObject == self) {
        return;
    }

    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = IOS7_NAVI_SPACE;
    self.navigationItem.leftBarButtonItems = @[flexSpacer,leftButtonItem];
}

- (void)removeNavBarLeftButtonItems
{
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.leftBarButtonItems = nil;
}

-(void)setNavBarRightButtonItem:(nonnull UIView *)rightButton
{
    if (rightButton) {
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
        flexSpacer.width = IOS7_NAVI_SPACE;
        self.navigationItem.rightBarButtonItems = @[flexSpacer,rightButtonItem];
    }
}

-(void)removeNavBarRightButtonItems
{
    self.navigationItem.rightBarButtonItem = nil;
    self.navigationItem.rightBarButtonItems = nil;
}

@end
